package com.mcintyret.site.service.journal;

import com.mcintyret.site.journal.Journal;
import com.mcintyret.site.journal.JournalFamily;
import com.mcintyret.site.journal.database.JournalDao;
import com.mcintyret.site.journal.database.JournalFamilyDao;
import com.mcintyret.site.web.controller.journal.forms.JournalForm;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * User: mcintyret2
 * Date: 13/04/2012
 */

@Service
public class JournalHelper {

    @Resource
    private JournalDao journalDao;
    @Resource
    private JournalFamilyDao journalFamilyDao;

    public int addNewJournalToJournalFamily(JournalForm journalForm) {
        Journal newJournal = createJournalFromJournalForm(journalForm);
        JournalFamily jf = journalFamilyDao.get(journalForm.getJournalFamilyId());
        jf.addJournal(newJournal);
        journalFamilyDao.flushSession();
        return newJournal.getId();
    }

    private Journal createJournalFromJournalForm(JournalForm form) {
        return new Journal(form.getName(), form.getDescription());
    }

    public void deleteJournalWithId(int journalId) {
        Journal journal = journalDao.get(journalId);
        journal.getJournalFamily().getJournals().remove(journal);
    }

    public Journal getJournalById(int id) {
        return journalDao.get(id);
    }
}
